package com.hoshiicloud.user.controller.usercenter;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.user.controller.BaseController;
import com.hoshiicloud.user.entity.usercenter.MemberFund;
import com.hoshiicloud.user.param.usercenter.MemberFundPageParam;
import com.hoshiicloud.user.param.usercenter.MemberFundUpdateParam;
import com.hoshiicloud.user.service.usercenter.IMemberFundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户资金流水Controller类
 *
 * @author : yangcheng
 * @date : 2019/5/11 18:49
 * @mail : maiboyc@163.com
 **/
@RestController
@RequestMapping("/memberFund")
@Api(value = "用户资金流水管理", tags = {"用户资金流水操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class MemberFundController extends BaseController {

    // 用户资金流水Service
    @Autowired
    private IMemberFundService memberFundService;

    /**
     * 根据ID查询用户资金流水
     * @param id ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询用户资金流水信息", consumes = MediaType.ALL_VALUE)
    public Response<MemberFund> getById(@PathVariable("id") Long id) {
        Response<MemberFund> rs;
        try {
            rs = Response.data(memberFundService.getById(id));
        } catch (Exception e) {
            logger.error("根据ID查询用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        }
        return rs;
    }

    /**
     * 根据ID组查询用户资金流水
     * @param ids ID组
     * @return Response
     */
    @GetMapping("/getByIds")
    @ApiOperation(value = "根据ID组查询", notes = "根据ID组查询用户资金流水信息", consumes = MediaType.ALL_VALUE)
    public Response<List<MemberFund>> getByIds(@RequestParam("ids") List<Long> ids) {
        Response<List<MemberFund>> rs;
        try {
            rs = Response.data(memberFundService.getByIds(ids));
        } catch (Exception e) {
            logger.error("根据ID组查询用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        }
        return rs;
    }

    /**
     * 根据资金流水编号查询用户资金流水
     * @param flowno  资金流水编号
     * @return Response
     */
    @GetMapping("/find/{flowno}")
    @ApiOperation(value = "根据资金流水编号查询", notes = "根据资金流水编号查询用户资金流水信息", consumes = MediaType.ALL_VALUE)
    public Response<IPage<MemberFund>> findByName(@PathVariable("flowno") String flowno) {
        Response<IPage<MemberFund>> rs;
        try {
            MemberFund entity = new MemberFund();
            entity.setFlowno(flowno);
            rs = Response.data(memberFundService.List(entity));
        } catch (Exception e) {
            logger.error("根据资金流水编号查询用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        }
        return rs;
    }

    /**
     * 分页查询用户资金流水（get请求，以具体值接收参数）
     * @param pageIndex         当前页码
     * @param pageSize          每页显示的记录数
     * @param userId            用户主键ID
     * @param flowno            资金流水编号
     * @param description       流水项目
     * @param dc                资金方向
     * @param source            来源
     * @param status            状态
     * @return Response
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询用户资金流水信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true, dataType = "Long", example = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true, dataType = "Long", example = "10", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户主键ID", dataType = "Long", example = "1", paramType = "query"),
            @ApiImplicitParam(name = "flowno", value = "资金流水编号", dataType = "String", example = "123", paramType = "query"),
            @ApiImplicitParam(name = "description", value = "流水项目", dataType = "String", example = "XX", paramType = "query"),
            @ApiImplicitParam(name = "dc", value = "资金方向", dataType = "String", example = "1", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", example = "1", paramType = "query"),
            @ApiImplicitParam(name = "source", value = "涞源", dataType = "Integer", example = "1", paramType = "query")
    })
    public Response<IPage<MemberFund>> list(Long pageIndex, Long pageSize, Long userId, String flowno, String description, String dc, Integer status, Integer source) {
        Response<IPage<MemberFund>> rs;
        try {
            MemberFund entity = new MemberFund();
            entity.setPageIndex(pageIndex);
            entity.setPageSize(pageSize);
            entity.setUserId(userId);
            entity.setFlowno(flowno);
            entity.setDescription(description);
            entity.setDc(dc);
            entity.setStatus(status);
            entity.setSource(source);
            rs = Response.data(memberFundService.List(entity));
        } catch (Exception e) {
            logger.error("分页查询用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        }
        return rs;
    }

    /**
     * 分页查询用户资金流水（post请求，以对象接收参数）
     * @param param 查询参数实体
     * @return Response
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询用户资金流水信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<IPage<MemberFund>> list(@RequestBody MemberFundPageParam param) {
        Response<IPage<MemberFund>> rs;
        try {
            MemberFund entity = new MemberFund();
            BeanUtils.copyProperties(param, entity);
            rs = Response.data(memberFundService.List(entity));
        } catch (Exception e) {
            logger.error("分页查询用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        }
        return rs;
    }

    /**
     * 新增用户资金流水
     * @param entity 保存参数实体
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增用户资金流水", notes = "新增用户资金流水信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> save(@RequestBody @Valid MemberFund entity, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                rs = Response.data(memberFundService.insert(entity));
            }
        } catch (JsonProcessingException e) {
            logger.error("处理JSON验证信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        } catch (ServiceException e) {
            logger.error("新增用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        }catch (Exception e) {
            logger.error("新增用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        }
        return rs;
    }

    /**
     * 修改用户资金流水
     * @param param 修改参数实体
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改用户资金流水", notes = "修改用户资金流水信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response<Long> updateById(@RequestBody @Valid MemberFundUpdateParam param, BindingResult result) {
        Response<Long> rs;
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                MemberFund entity = new MemberFund();
                BeanUtils.copyProperties(param, entity);
                rs = Response.data(memberFundService.update(entity));
            }
        } catch (JsonProcessingException e) {
            logger.error("处理JSON验证信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        } catch (ServiceException e) {
            logger.error("更新用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        } catch (Exception e) {
            logger.error("更新用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        }
        return rs;
    }

    /**
     * 根据ID删除用户资金流水
     * @param id 用户ID
     * @return Response
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除用户资金流水信息", consumes = MediaType.ALL_VALUE)
    public Response<Boolean> deleteById(@PathVariable("id") Long id) {
        Response rs;
        try {
            rs = Response.data(memberFundService.delete(id));
        } catch (ServiceException e) {
            logger.error("删除用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getError());
        } catch (Exception e) {
            logger.error("删除用户资金流水信息异常"+"["+e.getMessage()+"]");
            rs = Response.failed(e.getMessage());
        }
        return rs;
    }

}  